package com.situ.stl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.stl.dao.AdminMapper;
import com.situ.stl.entity.Admin;
import com.situ.stl.service.AdminService;
import com.situ.stl.util.MD5Util;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/** 
 * Service层 - 管理员
 */
@Service 
public class AdminServiceImpl implements AdminService{

    /** 
     */
    @Autowired 
    private AdminMapper adminMapper;

    /** 
     * add
     */
    @Override 
    public int add( Admin admin) throws Exception {
        Admin sAdmin = adminMapper.selectByPhone(admin.getPhone());
        if ( ! ObjectUtils.isEmpty(sAdmin)) {
            throw new Exception("手机号已存在！");
        }

        int res = adminMapper.insert(admin);
        return res;
    }

    /** 
     * remove
     */
    @Override 
    public int remove( int id) {
        int res = adminMapper.delete(id);
        return res;
    }

    /** 
     * edit
     */
    @Override 
    public int edit( Admin admin) throws Exception {
        if (StringUtils.hasText(admin.getPhone())){
            Admin sAdmin = adminMapper.selectByPhone(admin.getPhone());
            if ( ! ObjectUtils.isEmpty(sAdmin) && !admin.getId().equals(sAdmin.getId())) {
                throw new Exception("手机号已存在！");
            }
        }

        if (!ObjectUtils.isEmpty(admin.getPassword())) {
            admin.setPassword(MD5Util.getMD5(admin.getPassword()));
        }
        int res = adminMapper.update(admin);
        return res;
    }

    /** 
     * getById
     */
    @Override 
    public Admin getById( int id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    /** 
     * get All
     */
    @Override 
    public List<Admin> getAll() {
        return adminMapper.selectAll();
    }

    /** 
     * search
     */
    @Override 
    public List<Admin> search( Admin admin) {
        return adminMapper.select(admin);
    }

    /** 
     * searchByPage
     */
    @Override 
    public PageInfo searchByPage( Integer page, Integer limit, Admin admin) {
        if (page == null) page = 1;
        if (limit == null) limit = 10;
        
        PageHelper.startPage(page, limit);
        List list = adminMapper.select(admin);
        PageInfo pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /** 
     * login
     */
    @Override 
    public Admin login( Admin admin)  throws Exception{
        // 判断账号, 密码格式是否正确
        if (admin.getPhone() == null || admin.getPhone().length() <3 || admin.getPhone().length() > 16) {
        	throw new Exception("账号格式不正确");
        }
        if (admin.getPassword() == null || admin.getPassword().length() <3 || admin.getPassword().length() > 16) {
        	throw new Exception("密码格式不正确");
        }
        
        // 判断账号是否存在
        Admin sAdmin = adminMapper.selectByPhone(admin.getPhone());
        if (sAdmin == null) {
        	throw new Exception("用户名不存在");
        }
        
        // MD5加密
        String password = MD5Util.getMD5(admin.getPassword());
        // 判断密码是否正确
        if ( ! sAdmin.getPassword().equals(password)) {
        	throw new Exception("密码错误!");
        }
        
        // 其它判断
        return sAdmin;
    }

    /** 
     * register
     */
    @Override 
    public int reg( Admin admin, String repassword)  throws Exception{
        // 判断账号, 密码格式是否正确
        if (admin.getPhone() == null || admin.getPhone().length() <3 || admin.getPhone().length() > 16) {
        	throw new Exception("账号格式不正确");
        }
        if (admin.getPassword() == null || admin.getPassword().length() <3 || admin.getPassword().length() > 16) {
        	throw new Exception("密码格式不正确");
        }
        
        // 判断两次密码是否一致
        if ( ! admin.getPassword().equals(repassword)){
        	throw new Exception("两次密码不一致");
        }
        // 判断账号是否存在
        Admin admin2 = adminMapper.selectByPhone(admin.getPhone());
        if (admin2 != null) {
        	throw new Exception("用户名已存在");
        }
        
        // MD5加密
        admin.setPassword(MD5Util.getMD5(admin.getPassword()));
        // 其它操作
        
        return adminMapper.insert(admin);
    }

    @Override
    public Admin getByPhone(String phone) {
        return adminMapper.selectByPhone(phone);
    }

}